package cn.bdqn.witcity.controller;

import cn.bdqn.witcity.entity.Product;
import cn.bdqn.witcity.resposity.ProductDao;
import cn.bdqn.witcity.service.ProductService;
import cn.bdqn.witcity.util.ResponseResult;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ProductController {
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    ProductService productService;
    @Resource
    ProductDao productDao;




    @RequestMapping("/imports")
    public void imports(){
        List<Product> products=productService.getAllProduct();
        for(Product product:products){
            productDao.save(product);
        }
        for(Product product:products){
            redisTemplate.opsForValue().set(product.getId().toString(),product.getStock().toString());
        }
    }
    @RequestMapping("/shop/product/list")
    public ResponseResult productList(Integer page,Integer limit,String keyword){
        NativeSearchQueryBuilder nativeSearchQueryBuilder=new NativeSearchQueryBuilder();
        if(page==null){
            page=1;
        }
        if(limit==null){
            limit=10;
        }
        FieldSortBuilder sort= SortBuilders.fieldSort("price").order(SortOrder.DESC);
        if(keyword!=null && !keyword.equals("")){
            QueryBuilder queryBuilder= QueryBuilders.matchQuery("productName",keyword);
            nativeSearchQueryBuilder.withQuery(queryBuilder);
        }
        PageRequest pageable=PageRequest.of(page-1,limit);
        nativeSearchQueryBuilder.withSort(sort);
        nativeSearchQueryBuilder.withPageable(pageable);
        NativeSearchQuery query=nativeSearchQueryBuilder.build();
        Page<Product> page1=productDao.search(query);
        int count=(int)page1.getTotalElements();
        List<Product> productList=page1.getContent();
        return new ResponseResult(0,"",productList,count);
    }
    @RequestMapping("/addProduct")
    public ResponseResult addProduct(Product product){
        int result=productService.addProduct(product);
        if(result>0){
            List<Product> products=productService.getAllProduct();
            for(Product product1:products){
                productDao.save(product1);
            }
            redisTemplate.opsForValue().set(product.getId().toString(),product.getStock().toString());

            return new ResponseResult(200,"",result);
        }
        return new ResponseResult(0,"",result);
    }


}
